package com.leetcode.editor.cn.other;

import java.util.Arrays;

/**
 * @description: 排序算法练习
 * @author:
 * @create: 2020-12-04 14:59
 **/
public class Sort {
    public static void main(String[] args) {
        int[] arr = {8, 6, 3, 7, 2, 11, 5};
        //冒泡排序
//        bubbleSort(arr);
        //插入排序
        insertSort(arr);
        //快速排序
//        quickSort(arr, 0, arr.length - 1);
        //归并排序
//        arr = mergeSort(arr);
        System.out.println(Arrays.toString(arr));

    }

    private static int[] mergeSort(int[] arr) {
        if (arr.length <= 1) {
            return arr;
        }
        int mid = arr.length / 2;
        int[] left = Arrays.copyOfRange(arr, 0, mid);
        left = mergeSort(left);

        int[] right = Arrays.copyOfRange(arr, mid, arr.length);
        right = mergeSort(right);
        //合并
        return merge(left, right);
    }

    private static int[] merge(int[] l, int[] r) {
        int[] result = new int[l.length + r.length];
        int lIndex = 0, rIndex = 0;
        for (int i = 0; i < result.length; i++) {
            if (lIndex < l.length && rIndex < r.length) {
                if (l[lIndex] <= r[rIndex]) {
                    result[i] = l[lIndex++];
                } else {
                    result[i] = r[rIndex++];
                }
            } else if (lIndex < l.length) {
                result[i] = l[lIndex++];
            } else {
                result[i] = r[rIndex++];
            }
        }
        return result;
    }

    /**
     * 快速排序
     *
     * @param arr
     * @param low
     * @param hight
     */
    private static void quickSort(int[] arr, int low, int hight) {
        int i=low,j=hight;
        //基准位置
        int temp = arr[i];
        while(i<j){
            while(arr[j]>temp && i<j){
                j--;
            }
            while(arr[i]<temp && i<j){
                i++;
            }
            //交换i和j
            if(i!=j){
                int te=arr[i];
                arr[i]=arr[j];
                arr[j]=te;
            }
        }
        //交换i和基准位置
        arr[low]=arr[i];
        arr[i]=temp;
        //递归左右
        if(i-1>low){
            quickSort(arr,low,i-1);
        }
        if(i+1<hight){
            quickSort(arr,i+1,hight);
        }
    }

    /**
     * 插入排序
     *
     * @param arr
     */
    private static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            for (int j = i - 1; j >= 0; j--) {
                if (temp > arr[j]) {
                    arr[j + 1] = temp;
                    break;
                } else {
                    arr[j + 1] = arr[j];
                    if (j == 0) {
                        arr[j] = temp;
                    }
                }
                System.out.println(Arrays.toString(arr));
            }
        }
    }


    /**
     * 冒泡排序
     *
     * @param arr
     */
    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

}
